﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using Entity;
using Business; 

namespace WebApp
{
    public partial class FAQforAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Bindnews();
        }
        private void Bindnews()
        {

            GridView1.DataSource = GridDataSource();
            GridView1.DataBind();
        }
        private DataTable GridDataSource()
        {
            FAQ_Bussiness pb = new FAQ_Bussiness();
            DataTable dt = new DataTable();
            try
            {
                dt = pb.LoadP();
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
            finally
            {
                pb = null;
            }

            return dt;
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bindnews();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bindnews();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int faqID = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
            FAQ_Bussiness prBu = new FAQ_Bussiness();
            try
            {
                prBu.Delete(faqID);

                Label2.Text = "Record Deleted Successfully.";
            }
            catch (Exception ee)
            {
                Label2.Text = ee.Message.ToString();
            }
            finally
            {
                prBu = null;
            }

            GridView1.EditIndex = -1;
            // Refresh the list
            Bindnews();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            FAQ_Bussiness prB = new FAQ_Bussiness();
            GridViewRow row = GridView1.Rows[e.RowIndex];
            int faqID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            int getRowCount = 0;
            string ques = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string ans = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            getRowCount = prB.Update(faqID, ques, ans);
            GridView1.EditIndex = -1;
            Bindnews();
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            FAQ_Entity pr = new FAQ_Entity();
            FAQ_Bussiness Insert = new FAQ_Bussiness();

            pr.Ques = txtQues.Text.Trim();
            pr.Ans = txtAns.Text.Trim();


            Insert.Add_FAQ(pr);
            Bindnews();


            txtQues.Text = "";
            txtAns.Text = "";
        }

        protected void btnclear_Click(object sender, EventArgs e)
        {

            txtQues.Text = "";
            txtAns.Text = "";
        }

    }
}